package com.aro.view;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.HashMap;
import java.util.Map;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JTextField;
import javax.swing.WindowConstants;
import com.aro.controler.CtrlPersona;
import com.aro.controler.IControlable;

public class FrmPersona extends JFrame implements ActionListener, IDatable{
	
	private JTextField txtNombre;
	private JButton btnCancel;
	private JButton btnOK;
	private JLabel lblEdad;
	private JTextField txtEdad;
	private JLabel lblApellido;
	private JTextField txtApellido;
	private JLabel lblNombre;
	
	private IControlable controler;
	
	public FrmPersona(IControlable controler) {
		super();
		this.controler = controler;
		initGUI();
	}
	
	private void initGUI() {
		try {
			setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
			getContentPane().setLayout(null);
			{
				txtNombre = new JTextField();
				getContentPane().add(txtNombre);
				txtNombre.setBounds(115, 31, 224, 21);
			}
			{
				lblNombre = new JLabel();
				getContentPane().add(lblNombre);
				lblNombre.setText("Nombre");
				lblNombre.setBounds(39, 31, 64, 21);
			}
			{
				txtApellido = new JTextField();
				getContentPane().add(txtApellido);
				txtApellido.setBounds(115, 64, 224, 21);
			}
			{
				lblApellido = new JLabel();
				getContentPane().add(lblApellido);
				lblApellido.setText("Apellido");
				lblApellido.setBounds(39, 64, 58, 21);
			}
			{
				txtEdad = new JTextField();
				getContentPane().add(txtEdad);
				txtEdad.setBounds(115, 97, 85, 21);
			}
			{
				lblEdad = new JLabel();
				getContentPane().add(lblEdad);
				lblEdad.setText("Edad");
				lblEdad.setBounds(39, 100, 46, 18);
			}
			{
				btnOK = new JButton();
				getContentPane().add(btnOK);
				btnOK.setText("OK");
				btnOK.setBounds(191, 166, 63, 21);
				btnOK.addActionListener(this);
			}
			{
				btnCancel = new JButton();
				getContentPane().add(btnCancel);
				btnCancel.setText("Cancel");
				btnCancel.setBounds(265, 166, 74, 21);
				btnCancel.addActionListener(this);
			}
			pack();
			this.setSize(385, 255);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	@Override
	public void setData(Map<String, String> data) {
		this.txtNombre.setText(data.get(CtrlPersona.NAME));
		this.txtApellido.setText(data.get(CtrlPersona.SURNAME));
		this.txtEdad.setText(data.get(CtrlPersona.AGE));
	}

	@Override
	public Map<String, String> getData() {
		Map<String,String> data = new HashMap<String,String>();
		data.put(CtrlPersona.NAME, this.txtNombre.getText());
		data.put(CtrlPersona.SURNAME, this.txtApellido.getText());
		data.put(CtrlPersona.AGE, this.txtEdad.getText());
		return data;
	}

	@Override
	public void showMessage(String msj) {
		JOptionPane.showMessageDialog(null,msj,"Information",
				JOptionPane.INFORMATION_MESSAGE);
	}

	@Override
	public void actionPerformed(ActionEvent arg0) {
		if(arg0.getSource() == this.btnCancel){
			controler.cacelAction();
		}
		if(arg0.getSource() == this.btnOK){
			controler.oKAction();
		}
	}

	@Override
	public void close() {
		this.setVisible(false);
	}

	@Override
	public void showMe() {
		this.setVisible(true);
	}

}
